package com.thread.lesson.syn;

/**
 * @Author: Mr.Xi
 * @Description: 不安全的集合
 * @Date: Create in 11:08 2021/7/5
 * @Modified By:
 */

import java.util.ArrayList;
import java.util.List;

/**
 * 线程不安全的集合
 */
public class UnsafeList {
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        Thread.sleep(2000);
        System.out.println(list.size());//若线程安全，则集合大小为10000
    }
}
